package com.elephant.bean.lookupAndReplace.replace;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.MethodReplacer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

//@Component
public class UserCreatorMethodReplacer implements MethodReplacer {
    
    @Autowired
    private User userPrototype;
    
    @Override
    public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
        // 替换后的逻辑：每次返回一个新的 User 实例
//        User newUser = new User();
//        newUser.setName("Replaced User");
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("lookupAndReplace.xml");
		User userPrototype = applicationContext.getBean("userPrototype", User.class);
		userPrototype.setName("Replaced User");
        return this.userPrototype;
    }
}    